package com.tth.learn.addproperties.exec;

/**
 * @Author tth
 * @Date 2022/11/28 18:06
 */
public class StringTest {
    public static void main(String[] args) {
        System.out.println((int) Math.ceil((double) 6 / (double) 5));
        System.out.println((int) Math.floor((double) 6 / (double) 5));
//        System.out.println(Runtime.getRuntime().availableProcessors());
//        String str = "abcdefg";
//        try {
//            System.out.println(revert(str, 2, 6));
//        } catch (Exception e) {
//            e.getMessage();
//        }
//
//        System.out.println("***********************************");
//
//        str = "abkkcdkabkebfkabkskab";
//        System.out.println(getRepeatCount(str, "abk"));
//
//        System.out.println("***********************************");
//
//        str = "abcdefg";
//        System.out.println(getMaxChildString(str, "hefbhefgmn"));
//
//        System.out.println("***********************************");
//        System.out.println(Integer.MAX_VALUE);
    }

    /**
     * 获取两个字符串中的最大相同子串
     *
     * @param str1
     * @param str2
     * @return
     */
    public static String getMaxChildString(String str1, String str2) {
        if (str1.length() < str2.length()) {
            String temp = str1;
            str1 = str2;
            str2 = temp;
        }
        // 此时，可以保证str2是较小串
        for (int i = 0; i < str2.length(); i++) {
            for (int j = 0; j <= i; j++) {
                String temp = str2.substring(j, str2.length() - (i - j));
                if (str1.contains(temp)) {
                    return temp;
                }
            }
        }

        return "";
    }

    /**
     * 获取字符串在目标字符串中出现的次数
     *
     * @param target 目标字符串
     * @param str    字符串
     */
    public static int getRepeatCount(String target, String str) {
        if (str.length() > target.length()) {
            return 0;
        }

        int beginIndex = 0;
        int count = 0;
        while (target.indexOf(str, beginIndex) > -1) {
            count++;
            beginIndex = target.indexOf(str, beginIndex) + str.length();
        }

        return count;
    }

    /**
     * 字符串反转
     *
     * @param str
     * @param beginIndex
     * @param endIndex
     * @return
     * @throws Exception
     */
    public static String revert(String str, int beginIndex, int endIndex) throws Exception {
        if (beginIndex < 0) {
            throw new Exception("起始下标不得小于0");
        }
        if (beginIndex > endIndex) {
            throw new Exception("起始下标不得超过终止下标");
        }
        if (endIndex > str.length()) {
            throw new Exception("终止下标不得超过字符串长度");
        }
        endIndex--;
        char[] chars = str.toCharArray();
        for (int i = 0; i <= Math.ceil((endIndex - beginIndex) / 2); i++) {
            char temp = chars[i + beginIndex];
            chars[i + beginIndex] = chars[(endIndex - i)];
            chars[(endIndex - i)] = temp;
        }
        return new String(chars);
    }

}
